 /**
 * Copyright 2011 Joe Choi, All rights reserved.
 */
package tao.data.structure.tree;

import tao.data.structure.tree.Tree;

/**
 * To traverse a tree in Pre-Order
 *  
 * @author Joe Choi
 * @version 1.0.0
 * @see Traversal
 */
class PreOrderTraversal<V> extends AbstractTraversal<V> {
	public PreOrderTraversal(Tree<V> tree) {
		super(tree);
	}
	
	@Override
	protected void traverse(Tree<V> t) {
		notifyObserver(new Traversal.Event<V>(Traversal.Event.Type.TraversedTree, t));
		notifyObserver(new Traversal.Event<V>(Traversal.Event.Type.BeforeTraverseSubtree, t));
		for(Tree<V> child : t.getSubtree()) traverse(child);
		notifyObserver(new Traversal.Event<V>(Traversal.Event.Type.AfterTraverseSubtree, t));
	}

	/**
	 * To create a new PreOrderTraversal object
	 */
	static class Factory<V> extends AbstractFactory<V> {
		public Factory(Tree<V> tree) {
			super(tree);
		}
		
		@Override
		public Traversal<V> create() {
			return(new PreOrderTraversal<V>(getTree()));
		}
	}	
}